/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.DepartmentDAO;
import exatrez.dao.PenggunaDAO;
import exatrez.dao.ShipVisitComplianceDAO;
import exatrez.dao.ShipVisitCrewDAO;
import exatrez.dao.ShipVisitDescriptionDAO;
import exatrez.dao.ShipVisitFlowCounterDAO;
import exatrez.dao.ShipVisitFlowDAO;
import exatrez.dao.ShipVisitFlowFileDAO;
import exatrez.dao.ShipVisitFlowFileTmpDAO;
import exatrez.dao.VesselDAO;
import exatrez.manager.ShipVisitFlowManager;
import exatrez.model.InfoAudit;
import exatrez.model.ShipVisitCompliance;
import exatrez.model.ShipVisitCrew;
import exatrez.model.ShipVisitDescription;
import exatrez.model.ShipVisitFlow;
import exatrez.model.ShipVisitFlowCounter;
import exatrez.model.ShipVisitFlowFile;
import exatrez.model.ShipVisitFlowFileTmp;
import exatrez.model.ShipVisitFlowHistory;
import exatrez.model.User;
import exatrez.model.Vessel;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.Configuration;
import exatrez.stripes.ShipVisitConfiguration;
import exatrez.stripes.config.AuthRequired;
import exatrez.stripes.util.EmailUtil;
import exatrez.stripes.util.FileUtil;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.FileBean;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mansur
 */
@UrlBinding("/shipVisitFlow")
@AuthRequired
public class ShipVisitFlowAction extends BaseActionBean {
    
    private static Logger LOG = LoggerFactory.getLogger(ShipVisitFlowAction.class);
    
    @Inject
    ShipVisitConfiguration svconf;
    @Inject
    Configuration conf;
    @Inject
    ShipVisitFlowDAO shipVisitFlowDAO;
    @Inject
    ShipVisitFlowCounterDAO shipVisitFlowCounterDAO;
    @Inject
    private DepartmentDAO departmentDAO;
    @Inject
    private ShipVisitFlowManager svFlowManager;
    @Inject
    ShipVisitFlowFileTmpDAO shipVisitFlowFileTmpDAO;
    @Inject
    private EmailUtil emailUtil;
    @Inject
    ShipVisitDescriptionDAO shipVisitDescriptionDAO;
    @Inject
    ShipVisitComplianceDAO shipVisitComplianceDAO;
    @Inject
    PenggunaDAO userDAO;
    @Inject
    VesselDAO vesselDAO;
    @Inject
    ShipVisitCrewDAO shipVisitCrewDAO;
    @Inject
    ShipVisitFlowFileDAO shipVisitFlowFileDAO;
    
    private User userSelect;
    private User userCurrSession;
    private ShipVisitFlow shipVisitFlow;
    private ShipVisitFlowCounter shipVisitFlowCounter;
    private String currentYear = String.valueOf(new Date().getYear()+1900);
    private String departmentId;
    private Integer defaultCount = 0;
    private Integer counterId;
    private String shipVisitFlowId;
    private boolean superiorView = false;
    private boolean staffView = false;
    private boolean crewDetails = false;
    private String status;
    private String statusDesc;
    private String rejectNote;
    private String staffId;
    private FileBean document;
    private ShipVisitFlowFileTmp shipVisitFlowFileTmp;
    private String visitId;
    private Integer sizeDesc;
    private Integer sizeListCrew;
    private ShipVisitFlowFile shipVisitFlowFile;
    
    private List<ShipVisitFlowCounter> listCounter;
    private List<ShipVisitFlow> listShipVisitKIV;
    private List<ShipVisitFlow> listShipVisitApprove;
    private List<ShipVisitFlow> listShipVisitReject;
    private List<ShipVisitFlow> listStaffVisitKIV;
    private List<ShipVisitFlow> listStaffStatusDetails;
    private List<ShipVisitFlowFileTmp> listVisitFiletmp;
    private List<ShipVisitFlowFile> listVisitFile;
    private List<ShipVisitCrew> listShipVisitCrew;
    private List<ShipVisitDescription> listShipVisitDescription;
    private List<ShipVisitFlowHistory> listHistory;
    
    
    private List<String> compliances;
    private List<String> remarks;
    
    private List<ShipVisitCrew> listCrewCoo1;
    private List<ShipVisitCrew> listCrewCoo2;
    private List<ShipVisitCrew> listCrewCoo3;
    private List<ShipVisitCrew> listCrewCoo4;
    private List<ShipVisitCrew> listCrewCoo5;
    private List<ShipVisitCrew> listCrewCoo6;
    private List<ShipVisitCrew> listCrewCoo7;
    private List<ShipVisitCrew> listCrewCoo8;
    private List<ShipVisitCrew> listCrewCoo9;
    private List<ShipVisitCrew> listCrewCoo10;
    private List<ShipVisitCrew> listCrewCoo11;
    private List<ShipVisitCrew> listCrewCoo12;
    private List<ShipVisitCrew> listCrewCoo13;
    private List<ShipVisitCrew> listCrewCoo14;
    
    private static final String NEWVISITOTHER = "visitFlow/new_visit_other.jsp";
    private static final String OTHERVISITDETAIL = "visitFlow/verify_visit_other.jsp";
    private static final String EDITOTHER = "visitFlow/edit_visit_other.jsp";
    private static final String COOVISITOTHER = "visitFlow/coo_visit_other.jsp";
    
    private static final String NEWVISIT = "visitFlow/new_visit_super.jsp";
    private static final String VISITKIV = "visitFlow/kiv_visit_super.jsp";
    private static final String VISITAPPROVE = "visitFlow/approve_visit_super.jsp";
    private static final String VISITREJECT = "visitFlow/reject_visit_super.jsp";
    private static final String STAFFVISITKIV = "visitFlow/verify_visit_search.jsp";
    private static final String STAFFVISITDETAIL = "visitFlow/verify_visit_staff.jsp";
    private static final String EDITDOCUMENT = "visitFlow/edit_doc.jsp";
    private static final String VIEWDOCUMENT = "visitFlow/view_doc.jsp";
    private static final String EDITDOCPERMANENT = "visitFlow/edit_doc_permanent.jsp";
    private static final String SEARCHPASTVISIT = "visit/search_past_visit_staff.jsp";
    private static final String EDITSTAFF   = "visitFlow/edit_visit_staff.jsp";
    
    
    private static final String FOLDERNAME = "shipVisitFlow";
    
    private static final String NEWVISITCREW = "visitFlow/new_visit_crew.jsp";
    private static final String CREWVISITDETAIL = "visitFlow/verify_visit_crew.jsp";
    private static final String SEARCHVISITCOO = "visitFlow/search_visit_list.jsp";
    private static final String SEARCHVISITDETAIL = "visitFlow/search_visit_detail.jsp";
    private static final String COOVISITSTAFF = "visitFlow/coo_visit_staff.jsp";
    private static final String COOVISITCREW  = "visitFlow/coo_visit_crew.jsp";
    private static final String EDITCREW   = "visitFlow/edit_visit_crew.jsp";
    private String path;
    
    //    standard contenttype
    private String forXlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    private String forDocx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    private String forPptx = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    private String forXls = "application/vnd.ms-excel";
    private String forDoc = "application/msword";
    private String forPpt = "application/vnd.ms-powerpoint";
    private String forPdf = "application/pdf";
    private String forVideoMov = "video/quicktime";
    private String forVideoMkv = "video/x-matroska";
    private String forVideoMp4 = "video/mp4";
    private String forVideoAvi = "video/x-msvideo";
    private String forVideoMpeg= "video/mpeg";
    private String forVideoWmv = "application/octet-stream";
    private String forVideoAsf = "video/x-ms-asf";
    private String forVideoFlv = "video/x-flv";
    private String forVideoRmvb= "application/vnd.rn-realmedia-vbr";
    private String forVideo3gp = "video/3gpp";
    
    @DefaultHandler
    public Resolution newVisit(){
        String returnPage = NEWVISITOTHER;
        User user = getContext().getCurrentUser();
        LOG.debug("currentYear : "+currentYear);
        listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter);
        for (ShipVisitFlowCounter c : listCounter) {
            counterId = c.getSvFlowCounterId();
        }
        staffId = user.getUserId();
        if(user.getDepartment().getDepartmentName().contains(svconf.getProperty("user.crew.key"))){
            sizeDesc = svFlowManager.listShipVisitDescription().size();
            returnPage = NEWVISITCREW;
        }
        if(user.getDepartment().getDepartmentName().contains(svconf.getProperty("user.super.key")) || 
                user.getDepartment().getDepartmentName().contains(svconf.getProperty("user.procurement.key"))){
            returnPage = NEWVISIT;
        }
        return new JSP(returnPage);
    }
    
    private void checkListCounter(List<ShipVisitFlowCounter> list){        
        String result = null;
        User user = getContext().getCurrentUser();
        if(list.size() == 0){
            ShipVisitFlowCounter visitCounter = new ShipVisitFlowCounter();
            visitCounter.setUser(user);
            visitCounter.setVisited(defaultCount);
            visitCounter.setRejected(defaultCount);
            visitCounter.setYear(currentYear);
            visitCounter.setTarget(departmentDAO.findById(user.getDepartment().getDepartmentId()).getTarget());
            
            InfoAudit ia = new InfoAudit();
            ia.setCreateId(user);
            ia.setCreateDate(new Date());
            visitCounter.setInfoAudit(ia);
            result = svFlowManager.saveOrUpdateVisitCounter(visitCounter);
            LOG.debug("(checkListCounter)result new visitCounter :"+result);
            listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
        }else{
            if(list.get(0).getTarget().compareTo(user.getDepartment().getTarget()) == 0){
                result = "bingo.. the value is pair";
                LOG.debug("(checkListCounter)result visitCounter :"+result);
            }else{
                shipVisitFlowCounter = shipVisitFlowCounterDAO.findById(listCounter.get(0).getSvFlowCounterId());
                shipVisitFlowCounter.setTarget(user.getDepartment().getTarget());
                 InfoAudit iaU = shipVisitFlowCounter.getInfoAudit();
                 iaU.setUpdateDate(new Date());
                 iaU.setUpdateId(user);
                result = svFlowManager.saveOrUpdateVisitCounter(shipVisitFlowCounter);
                LOG.debug("(checkListCounter)result update visitCounter :"+result);
                listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
            }
        }
    }
    
    public Resolution submit(){
        User user = getContext().getCurrentUser();
        LOG.debug("user.id :"+user.getUserId());
        
        InfoAudit ia = new InfoAudit();
        ia.setCreateDate(new Date());
        ia.setCreateId(user);
        try{
            ShipVisitFlow svf = new ShipVisitFlow();
            svf.setDeleted("N");
            svf.setStatus("K");
            svf.setUser(user);
            svf.setCurrentDepartment(user.getDepartment().getDepartmentName());
            svf.setInfoAudit(ia);
            svf.setDateVisit(shipVisitFlow.getDateVisit());
            svf.setVessel(shipVisitFlow.getVessel());
            svf.setLocation(shipVisitFlow.getLocation());
            if(user.getDepartment().getDepartmentName().contains(svconf.getProperty("user.super.key")) || 
                user.getDepartment().getDepartmentName().contains(svconf.getProperty("user.procurement.key"))){
                svf.setAccomodation(shipVisitFlow.getAccomodation());
                svf.setConclusion(shipVisitFlow.getConclusion());
                svf.setCrewMorale(shipVisitFlow.getCrewMorale());
                svf.setDeck(shipVisitFlow.getDeck());
                svf.setEngine(shipVisitFlow.getEngine());
                svf.setFoodMeal(shipVisitFlow.getFoodMeal());
                svf.setGeneralCondition(shipVisitFlow.getGeneralCondition());
                svf.setOther(shipVisitFlow.getOther());
                svf.setPurpose(shipVisitFlow.getPurpose());
                svf.setRecommend(shipVisitFlow.getRecommend());
                svf.setSummary(shipVisitFlow.getSummary());
            }else{
                svf.setDescription(shipVisitFlow.getDescription());
            }
            List<ShipVisitFlowFileTmp> listFiletmp = new ArrayList<ShipVisitFlowFileTmp>();
            listFiletmp = svFlowManager.getListFiletmp(user.getUserId());
            String result = svFlowManager.saveOrUpdateVisit(svf, listFiletmp);
            if(result.equals("success")){
//                this.sendMailCoo(svf);
                addSimpleMessage(svconf.getProperty("super.coo.success"));
            }else{
                addSimpleError(svconf.getProperty("super.coo.error"));
            }
        }catch (Exception ex){
            LOG.error("submit error : "+ex.getMessage());
            ex.printStackTrace(); // for developer used only
            addSimpleError(svconf.getProperty("super.coo.err.ex"));
        }
        return new RedirectResolution(ShipVisitFlowAction.class,"visitKiv");
    }
    
    public Resolution visitKiv(){
        User user = getContext().getCurrentUser();
        userCurrSession = user;
        LOG.debug("currentYear : "+currentYear);
        listShipVisitKIV = svFlowManager.getListVisitKIV(user, currentYear);
        listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter);
        for (ShipVisitFlowCounter c : listCounter) {
            counterId = c.getSvFlowCounterId();
        }
        return new JSP(VISITKIV);
    }
    
    public Resolution visitApprove(){
        User user = getContext().getCurrentUser();
        userCurrSession = user;
        LOG.debug("currentYear : "+currentYear);
        listShipVisitApprove = svFlowManager.getListVisitApproved(user, currentYear);
        listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter);
        for (ShipVisitFlowCounter c : listCounter) {
            counterId = c.getSvFlowCounterId();
        }
        return new JSP(VISITAPPROVE);
    }
    
    public Resolution visitReject(){
        User user = getContext().getCurrentUser();
        userCurrSession = user;
        LOG.debug("currentYear : "+currentYear);
        listShipVisitReject = svFlowManager.getListVisitRejected(user, currentYear);
        listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter);
        for (ShipVisitFlowCounter c : listCounter) {
            counterId = c.getSvFlowCounterId();
        }
        return new JSP(VISITREJECT);
    }
    
    public Resolution staffDetail(){
        String returnPage = OTHERVISITDETAIL;
        User user = getContext().getCurrentUser();
        shipVisitFlowId = getContext().getRequest().getParameter("idVisit");
        int svfId = Integer.parseInt(shipVisitFlowId);
        shipVisitFlow = shipVisitFlowDAO.findById(svfId);
        this.setSuperiorView(false);
        this.setStaffView(true);
        if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.crew.key"))){
            this.initCrewList(shipVisitFlowId);
            returnPage = CREWVISITDETAIL;
        }
        if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
            returnPage = STAFFVISITDETAIL;
        }
        listHistory = svFlowManager.listHistory(String.valueOf(shipVisitFlow.getShipVisitFlowId()));
        LOG.debug("staffDetail : listHistory.size : "+listHistory.size());
        return new JSP(returnPage);      
    }
    
    public Resolution staffVisitKIV(){
        User user = getContext().getCurrentUser();
        listStaffVisitKIV = svFlowManager.getAllListStaffVisitKIV();
        LOG.debug("listStaffVisitKIV.size :"+listStaffVisitKIV.size());
        return new JSP(STAFFVISITKIV);
    }
    
    public Resolution staffVisitDetail(){
        String returnPage = OTHERVISITDETAIL;
        User user = getContext().getCurrentUser();
        shipVisitFlowId = getContext().getRequest().getParameter("idVisit");
        int svfId = Integer.parseInt(shipVisitFlowId);
        shipVisitFlow = shipVisitFlowDAO.findById(svfId);
        this.setSuperiorView(true);
        this.setStaffView(false);
        if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.crew.key"))){
            this.initCrewList(shipVisitFlowId);
            returnPage = CREWVISITDETAIL;
        }
        if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
            returnPage = STAFFVISITDETAIL;
        }
        listHistory = svFlowManager.listHistory(String.valueOf(shipVisitFlow.getShipVisitFlowId()));
        LOG.debug("staffVisitDetail : listHistory.size : "+listHistory.size());
        return new JSP(returnPage);        
    }
    
    public Resolution submitBack(){
        String returnPage = OTHERVISITDETAIL;
        User user = getContext().getCurrentUser();
        LOG.debug("shipVisitFlowId = "+shipVisitFlowId);
        int svfId = Integer.parseInt(shipVisitFlowId);
        shipVisitFlow = shipVisitFlowDAO.findById(svfId);
        shipVisitFlow.setVerifierId(user);
        LOG.debug("status :"+status);
        if(StringUtils.isBlank(status) || status.length() == 0){
            addSimpleError(svconf.getProperty("coo.blank.err"));
            this.setSuperiorView(true);
            this.setStaffView(false);
            if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.crew.key"))){
                this.initCrewList(shipVisitFlowId);
                returnPage = CREWVISITDETAIL;
            }
            if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
                returnPage = STAFFVISITDETAIL;
            }
            return new JSP(returnPage);
        }
        if(status.equals("R") && StringUtils.isBlank(rejectNote)){
            addSimpleError(svconf.getProperty("coo.reject.err"));
            this.setSuperiorView(true);
            this.setStaffView(false);
            if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.crew.key"))){
                this.initCrewList(shipVisitFlowId);
                returnPage = CREWVISITDETAIL;
            }
            if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
                returnPage = STAFFVISITDETAIL;
            }
            return new JSP(returnPage);
        }
        shipVisitFlow.setStatus(status);
        shipVisitFlow.setRejectNote(rejectNote);
        InfoAudit ia = shipVisitFlow.getInfoAudit();
        ia.setUpdateDate(new Date());
        ia.setUpdateId(user);
        shipVisitFlow.setInfoAudit(ia);
        try{
            String result = svFlowManager.saveOrUpdateVisit(shipVisitFlow);
            if(result.equals("success")){
                LOG.debug("status selected : "+shipVisitFlow.getStatus());
                if(shipVisitFlow.getStatus().equals("A")){
                    shipVisitFlowCounter = svFlowManager.getListCounter(shipVisitFlow.getUser(), currentYear, departmentId).get(0);
                    InfoAudit iac = shipVisitFlowCounter.getInfoAudit();
                    iac.setUpdateId(user);
                    iac.setUpdateDate(new Date());
                    LOG.info("shipVisitFlowCounter.id :"+shipVisitFlowCounter.getSvFlowCounterId()+", shipVisitFlowCounter.visited :"+shipVisitFlowCounter.getVisited());
                    shipVisitFlowCounter.setVisited(shipVisitFlowCounter.getVisited()+1);
                    shipVisitFlowCounter.setInfoAudit(iac);
                    svFlowManager.saveOrUpdateVisitCounter(shipVisitFlowCounter);
                    this.sendMailStaff(shipVisitFlow);
                }
                if(shipVisitFlow.getStatus().equals("R")){
                    shipVisitFlowCounter = svFlowManager.getListCounter(shipVisitFlow.getUser(), currentYear, departmentId).get(0);
                    InfoAudit iac = shipVisitFlowCounter.getInfoAudit();
                    iac.setUpdateId(user);
                    iac.setUpdateDate(new Date());
                    LOG.info("shipVisitFlowCounter.id :"+shipVisitFlowCounter.getSvFlowCounterId()+", shipVisitFlowCounter.rejected :"+shipVisitFlowCounter.getRejected());
                    shipVisitFlowCounter.setRejected(shipVisitFlowCounter.getRejected()+1);
                    shipVisitFlowCounter.setInfoAudit(iac);
                    svFlowManager.saveOrUpdateVisitCounter(shipVisitFlowCounter);
                    this.sendMailStaff(shipVisitFlow);
                }
//                this.sendMailStaff(shipVisitFlow);
                addSimpleMessage(svconf.getProperty("success"));
                ShipVisitFlowHistory his = new ShipVisitFlowHistory();
                his.setShipVisitFlow(shipVisitFlow);
                his.setStatus(shipVisitFlow.getStatus());
                his.setRejectNote(shipVisitFlow.getRejectNote());
                his.setVerifierId(user);
                InfoAudit iah = new InfoAudit();
                iah.setCreateId(user);
                iah.setCreateDate(new Date());
                his.setInfoAudit(iah);
                svFlowManager.saveHistory(his);
            }else{
                addSimpleError(svconf.getProperty("super.coo.error"));
                if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.crew.key"))){
                    this.initCrewList(shipVisitFlowId);
                    returnPage = CREWVISITDETAIL;
                }
                if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                    shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
                    returnPage = STAFFVISITDETAIL;
                }
                return new JSP(returnPage);
            }
        }catch(Exception ex){
            LOG.debug("submitBack.err :"+ex.getMessage());
            ex.printStackTrace(); // for development used only
            addSimpleError(svconf.getProperty("upload.notvalid"));
            if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.crew.key"))){
                this.initCrewList(shipVisitFlowId);
                returnPage = CREWVISITDETAIL;
            }
            if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
                returnPage = STAFFVISITDETAIL;
            }
            return new JSP(returnPage);
        }        
        return new RedirectResolution(ShipVisitFlowAction.class,"staffVisitKIV");
    }
    
    public Resolution editDocument(){
        staffId = getContext().getRequest().getParameter("idStaff");
        if(StringUtils.isNotBlank(staffId)){
            listVisitFiletmp = svFlowManager.getListFiletmp(staffId);
            LOG.debug("listVisitFiletmp.size :"+listVisitFiletmp.size());
        }
        
        return new JSP(EDITDOCUMENT).addParameter("popup", true);
    }
    
    public Resolution saveDoc(){
        User user = getContext().getCurrentUser();
        SimpleDateFormat dt1 = new SimpleDateFormat("ddMMyyyy");
        String documentPath = conf.getProperty("document.path");
        InfoAudit ia = new InfoAudit();
        LOG.info("documentPath :"+documentPath);
        Date date = new Date();
        
        String fulldate = dt1.format(date);
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + fulldate + File.separator;
        String docPath = FOLDERNAME + File.separator + fulldate + File.separator;
        
        LOG.info("path :"+path);
        LOG.info("docPath :"+docPath);
        
        if(document != null){
            FileUtil fileUtil = new FileUtil(path);
            LOG.debug("fileUtil : pass");
            try{
                String filename = fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                LOG.debug("filename : "+filename);
                ShipVisitFlowFileTmp visitFile = new ShipVisitFlowFileTmp();
                visitFile.setDeleted("N");
                visitFile.setDocumentPath(docPath + document.getFileName());
                visitFile.setTitle(document.getFileName());
                visitFile.setUser(user);
                visitFile.setContentType(document.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(user);
                visitFile.setInfoAudit(ia);
                LOG.debug("file title :"+visitFile.getTitle());
                String result = svFlowManager.saveorUpdateVisitFiletmp(visitFile);
                if(result.equals("success"))
                    addSimpleMessage(svconf.getProperty("upload.success"));
                else
                    addSimpleError(svconf.getProperty("upload.error"));
            }catch (Exception ex) {
                ex.printStackTrace();
//                LOG.error("(shipVisitFlowAction) error while saving file {}", ex.getCause().getMessage());
                addSimpleError(svconf.getProperty("upload.notvalid"));
            }
        }
        return new RedirectResolution(ShipVisitFlowAction.class, "editDocument")
                .addParameter("idStaff", user.getUserId());
    }
    
    public Resolution deleteFile(){
        staffId = getContext().getRequest().getParameter("idStaff");
        String fileId = getContext().getRequest().getParameter("idFile");
        
        if(StringUtils.isNotBlank(fileId)){
            shipVisitFlowFileTmp = shipVisitFlowFileTmpDAO.findById(Integer.parseInt(fileId));
            shipVisitFlowFileTmp.setDeleted("Y");
            
            InfoAudit ia = shipVisitFlowFileTmp.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(getContext().getCurrentUser());
            shipVisitFlowFileTmp.setInfoAudit(ia);
            String result = svFlowManager.saveorUpdateVisitFiletmp(shipVisitFlowFileTmp);
            if(result.equals("success"))
                addSimpleMessage(svconf.getProperty("delete.success"));
            else
                addSimpleError(svconf.getProperty("delete.error"));
        }else{
            addSimpleError(svconf.getProperty("delete.idnull"));
        }
        
        return editDocument();
    }
    
    public Resolution viewDoc(){
        visitId = getContext().getRequest().getParameter("idVisit");
        if(StringUtils.isNotBlank(visitId)){
            listVisitFile = shipVisitFlowDAO.findById(Integer.parseInt(visitId)).getListFilteredFile();
        }
        return new JSP(VIEWDOCUMENT).addParameter("popup", true);
    }
    
    public Resolution editDocPermanent(){
        visitId = getContext().getRequest().getParameter("idVisit");
        LOG.debug("editDocPermanent : visitId :"+visitId);
        if(StringUtils.isNotBlank(visitId)){
            listVisitFile = shipVisitFlowDAO.findById(Integer.parseInt(visitId)).getListFilteredFile();
        }
        LOG.debug("editDocPermanent : listVisitFile :"+listVisitFile.size());
        return new JSP(EDITDOCPERMANENT).addParameter("popup", true);
    }
    
    public Resolution saveDocPermanent(){
        LOG.debug("visitId :"+visitId);
        User user = getContext().getCurrentUser();
        SimpleDateFormat dt1 = new SimpleDateFormat("ddMMyyyy");
        String documentPath = conf.getProperty("document.path");
        InfoAudit ia = new InfoAudit();
        LOG.info("documentPath :"+documentPath);
        Date date = new Date();
        
        String fulldate = dt1.format(date);
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + fulldate + File.separator;
        String docPath = FOLDERNAME + File.separator + fulldate + File.separator;
        
        LOG.info("path :"+path);
        LOG.info("docPath :"+docPath);
        
        ShipVisitFlow svf = shipVisitFlowDAO.findById(Integer.parseInt(visitId));
        
        if(document != null){
            FileUtil fileUtil = new FileUtil(path);
            LOG.debug("fileUtil : pass");
            try{
                String filename = fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                LOG.debug("filename : "+filename);
                ShipVisitFlowFile visitFile = new ShipVisitFlowFile();
                visitFile.setDeleted("N");
                visitFile.setDocumentPath(docPath + document.getFileName());
                visitFile.setTitle(document.getFileName());                
                visitFile.setShipVisitFlow(svf);
                visitFile.setContentType(document.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(user);
                visitFile.setInfoAudit(ia);
                LOG.debug("file title :"+visitFile.getTitle());
                String result = svFlowManager.saveorUpdateVisitFile(visitFile);
                if(result.equals("success"))
                    addSimpleMessage(svconf.getProperty("upload.success"));
                else
                    addSimpleError(svconf.getProperty("upload.error"));
            }catch (Exception ex) {
                ex.printStackTrace();
//                LOG.error("(shipVisitFlowAction) error while saving file {}", ex.getCause().getMessage());
                addSimpleError(svconf.getProperty("upload.notvalid"));
            }
        }
        return new RedirectResolution(ShipVisitFlowAction.class, "editDocPermanent")
                .addParameter("idVisit", visitId);
    }
    
    public Resolution deleteFilePermanent(){
        visitId = getContext().getRequest().getParameter("idVisit");
        String fileId = getContext().getRequest().getParameter("idFile");
        
        if(StringUtils.isNotBlank(fileId)){
            shipVisitFlowFile = shipVisitFlowFileDAO.findById(Integer.parseInt(fileId));
            shipVisitFlowFile.setDeleted("Y");
            
            InfoAudit ia = shipVisitFlowFile.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(getContext().getCurrentUser());
            shipVisitFlowFile.setInfoAudit(ia);
            String result = svFlowManager.saveorUpdateVisitFile(shipVisitFlowFile);
            if(result.equals("success"))
                addSimpleMessage(svconf.getProperty("delete.success"));
            else
                addSimpleError(svconf.getProperty("delete.error"));
        }else{
            addSimpleError(svconf.getProperty("delete.idnull"));
        }
        
        return editDocPermanent();
    }
    
    private void sendMailCoo(ShipVisitFlow svf){
        if(svf != null){
            try{
                // freemarker stuff.
                freemarker.template.Configuration cfg = new freemarker.template.Configuration();
                DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
                FileTemplateLoader templateLoader 
                        = new FileTemplateLoader(new File(conf.getProperty("email.template.path")));
                cfg.setTemplateLoader(templateLoader);
                cfg.setObjectWrapper(new DefaultObjectWrapper());
                Template template = cfg.getTemplate("html-sv-coo.ftl");
                Map<String, Object> rootMap = new HashMap<String, Object>();
                rootMap.put("staffid", svf.getUser().getUserId());
                rootMap.put("department", svf.getUser().getDepartment().getDepartmentName());
                rootMap.put("vesselname", svf.getVessel().getVesselName());
                rootMap.put("location", svf.getLocation());
                rootMap.put("datevisit", formatter.format(svf.getDateVisit()));
                rootMap.put("inside", conf.getProperty("task.email.inside"));
                rootMap.put("outside", conf.getProperty("task.email.outside"));
                
                Writer out = new StringWriter();
                template.process(rootMap, out);
                String [] recipient = conf.getProperty("email.ptask.coo").split(",");
                emailUtil.send(recipient, null, svconf.getProperty("email.coo"), out.toString(), "text/html");
                // freemarker stuff ends.
            }catch(Exception ex){
                LOG.error("error occured {}!! ", ex);
            }
        }else{
            LOG.error("Ship Visit is NULL!!");
        }
    }
    
    private void sendMailStaff(ShipVisitFlow svf){
        if(svf != null){
            try{
                String status = null;
                if(svf.getStatus().equals("R")){
                    status = "REJECTED";
                }
                if(svf.getStatus().equals("A")){
                    status = "APPROVED";
                }
                // freemarker stuff.
                freemarker.template.Configuration cfg = new freemarker.template.Configuration();
                DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
                FileTemplateLoader templateLoader 
                        = new FileTemplateLoader(new File(conf.getProperty("email.template.path")));
                cfg.setTemplateLoader(templateLoader);
                cfg.setObjectWrapper(new DefaultObjectWrapper());
                Template template = cfg.getTemplate("html-sv-staff.ftl");
                Map<String, Object> rootMap = new HashMap<String, Object>();
                rootMap.put("vesselname", svf.getVessel().getVesselName());
                rootMap.put("location", svf.getLocation());
                rootMap.put("datevisit", formatter.format(svf.getDateVisit()));
                rootMap.put("status", status);
                rootMap.put("remarks", svf.getRejectNote());
                rootMap.put("inside", conf.getProperty("task.email.inside"));
                rootMap.put("outside", conf.getProperty("task.email.outside"));
                
                Writer out = new StringWriter();
                template.process(rootMap, out);
                String [] recipient = {svf.getUser().getEmail()};
                LOG.debug("sendMailStaff : status :"+status);
                emailUtil.send(recipient, null, svconf.getProperty("email.staff"), out.toString(), "text/html");
                // freemarker stuff ends.
            }catch(Exception ex){
                LOG.error("error occured {}!! ", ex);
            }
        }else{
            LOG.error("Ship Visit is NULL!!");
        }
    }
    
    private void initCrewList(String svfId){
//        listCrewCoo1 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo2 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo3 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo4 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo5 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo6 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo7 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo8 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo9 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo10 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo11 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo12 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo13 = new ArrayList<ShipVisitCrew>();
//        listCrewCoo14 = new ArrayList<ShipVisitCrew>();
        listCrewCoo1 = svFlowManager.listCrew("1", svfId);
        listCrewCoo2 = svFlowManager.listCrew("2", svfId);
        listCrewCoo3 = svFlowManager.listCrew("3", svfId);
        listCrewCoo4 = svFlowManager.listCrew("4", svfId);
        listCrewCoo5 = svFlowManager.listCrew("5", svfId);
        listCrewCoo6 = svFlowManager.listCrew("6", svfId);
        listCrewCoo7 = svFlowManager.listCrew("7", svfId);
        listCrewCoo8 = svFlowManager.listCrew("8", svfId);
        listCrewCoo9 = svFlowManager.listCrew("9", svfId);
        listCrewCoo10 = svFlowManager.listCrew("10", svfId);
        listCrewCoo11 = svFlowManager.listCrew("11", svfId);
        listCrewCoo12 = svFlowManager.listCrew("12", svfId);
        listCrewCoo13 = svFlowManager.listCrew("13", svfId);
        listCrewCoo14 = svFlowManager.listCrew("14", svfId);
    }
    
    public Resolution saveCrew(){
        User user = getContext().getCurrentUser();
        LOG.debug("user.id :"+user.getUserId());
        
        LOG.debug("currentYear : "+currentYear);
        listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter);
        for (ShipVisitFlowCounter c : listCounter) {
            counterId = c.getSvFlowCounterId();
        }
        staffId = user.getUserId();
        
        InfoAudit ia = new InfoAudit();
        ia.setCreateDate(new Date());
        ia.setCreateId(user);
        
        try{
            ShipVisitFlow svf = new ShipVisitFlow();
            svf.setDeleted("N");
            svf.setStatus("I"); // I = Incompleted
            svf.setUser(user);
            svf.setCurrentDepartment(user.getDepartment().getDepartmentName());
            svf.setInfoAudit(ia);
            svf.setDateVisit(shipVisitFlow.getDateVisit());
            svf.setVessel(shipVisitFlow.getVessel());
            svf.setLocation(shipVisitFlow.getLocation());
            svf.setMasterName(shipVisitFlow.getMasterName());
            svf.setImoNumber(shipVisitFlow.getImoNumber());
            svf.setGrossTonnage(shipVisitFlow.getGrossTonnage());
            
            List<ShipVisitFlowFileTmp> listFiletmp = new ArrayList<ShipVisitFlowFileTmp>();
            listFiletmp = svFlowManager.getListFiletmp(user.getUserId());
            String result = svFlowManager.saveOrUpdateVisit(svf, listFiletmp);
            if(result.equals("success")){
                this.setCrewDetails(true);
                addSimpleMessage(svconf.getProperty("success"));
                LOG.debug("saveCrew.svfId = "+svf.getShipVisitFlowId());
                this.shipVisitFlowId = String.valueOf(svf.getShipVisitFlowId());                
            }else{
                addSimpleError(svconf.getProperty("super.coo.error"));
            }
        }catch(Exception ex){
            ex.printStackTrace(); // for development used only
            LOG.error("saveCrew : error ex : "+ex.getMessage());
            addSimpleError(svconf.getProperty("super.coo.err.ex"));
        }        
        sizeDesc = svFlowManager.listShipVisitDescription().size();
        return new JSP(NEWVISITCREW);
    }
    
    public Resolution saveCrewSend(){
        User user = getContext().getCurrentUser();
        LOG.debug("currentYear : "+currentYear);
        listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter);
        for (ShipVisitFlowCounter c : listCounter) {
            counterId = c.getSvFlowCounterId();
        }
        staffId = user.getUserId();
        LOG.debug("user.id :"+user.getUserId());
        
        InfoAudit ia = new InfoAudit();
        ia.setCreateDate(new Date());
        ia.setCreateId(user);
        
        sizeDesc = svFlowManager.listShipVisitDescription().size();
        LOG.debug("shipVisitFlowId = "+this.shipVisitFlowId);
        LOG.debug("shipVisitDescription.size : "+this.svFlowManager.listShipVisitDescription().size());
        
        listShipVisitDescription = svFlowManager.listShipVisitDescription();
        for (ShipVisitDescription desc : listShipVisitDescription) {
            LOG.debug("desc.ID :"+desc.getSvDescId()+", sortingNumber :"+desc.getSortNumber());
        }        
        LOG.debug("compliances.size :"+this.compliances.size());
        LOG.debug("remarks.size :"+this.remarks.size());
        
        if(this.compliances == null || this.compliances.size() != listShipVisitDescription.size()){
            addSimpleError("All compliances field for each tab's must be fill..!!");
            this.setCrewDetails(true);
            return new JSP(NEWVISITCREW);
        }
        if(this.remarks == null || this.remarks.size() != listShipVisitDescription.size()){
            addSimpleError("All remarks field for each tab's must be fill..!!");
            this.setCrewDetails(true);
            return new JSP(NEWVISITCREW);
        }
        try{
            ShipVisitFlow svf = shipVisitFlowDAO.findById(Integer.parseInt(this.shipVisitFlowId));
            svf.setSummary(shipVisitFlow.getSummary());
            svf.setStatus("K"); // K = KIV
            InfoAudit iao = svf.getInfoAudit();
            iao.setUpdateId(user);
            iao.setUpdateDate(new Date());
            svf.setInfoAudit(iao);
            
            List<ShipVisitCrew> listCrew = new ArrayList<ShipVisitCrew>();
            int i = 0;
            for (ShipVisitDescription svd : listShipVisitDescription) {
                ShipVisitCrew svc = new ShipVisitCrew();
                svc.setSvDescId(svd);
                svc.setSvCompId(shipVisitComplianceDAO.findById(this.compliances.get(i)));
                svc.setShipVisitFlowId(svf);
                svc.setRemarks(this.remarks.get(i));
                svc.setInfoAudit(ia);
                listCrew.add(svc);
                i++;
            }
            
            LOG.debug("listCrew.size :"+listCrew.size());
            
            String result = svFlowManager.updateAndSaveCrew(svf, listCrew);
            if(result.equals("success")){
                addSimpleMessage(svconf.getProperty("super.coo.success"));
            }else{
                addSimpleError(svconf.getProperty("super.coo.error"));
                this.setCrewDetails(true);
                return new JSP(NEWVISITCREW);
            }            
        }catch(Exception ex){
            ex.printStackTrace(); // for development used only
            LOG.equals("saveCrewSend : ex : "+ex.getMessage());
            addSimpleError(svconf.getProperty("super.coo.err.ex"));
            this.setCrewDetails(true);
            return new JSP(NEWVISITCREW);
        }        
        
        return new RedirectResolution(ShipVisitFlowAction.class,"visitKiv");
    }
    
    public Resolution searchStaffVisit(){
        User user = getContext().getCurrentUser();
        LOG.debug("currentYear : "+currentYear);
        listCounter = svFlowManager.getListCounter(null, currentYear, departmentId);
        LOG.debug("listCounter :"+listCounter.size());
        return new JSP(SEARCHVISITCOO);
    }
    
    public Resolution searchStaffVisitDetail(){
        User user = getContext().getCurrentUser();
        LOG.debug("currentYear : "+currentYear);
        LOG.debug("staffId : "+staffId);
        LOG.debug("status : "+status);
        try{
            if(status.equals("A"))
                statusDesc = "Approved";
            if(status.equals("R"))
                statusDesc = "Rejected";
            userSelect = userDAO.findById(staffId);
            listStaffStatusDetails = svFlowManager.listApproveReject(staffId, status, currentYear);
        }catch(Exception ex){
            ex.printStackTrace(); // for development used only
            LOG.error("searchStaffVisitDetail : ex : "+ex.getMessage());
            addSimpleError(svconf.getProperty("super.coo.err.ex"));
            return new JSP(SEARCHVISITCOO);
        }
        
        return new JSP(SEARCHVISITDETAIL);
    }
    
    public Resolution staffDetailCoo(){
        String returnPage = COOVISITOTHER;
        User user = getContext().getCurrentUser();
        shipVisitFlowId = getContext().getRequest().getParameter("idVisit");
        int svfId = Integer.parseInt(shipVisitFlowId);
        shipVisitFlow = shipVisitFlowDAO.findById(svfId);
        if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.crew.key"))){
            this.initCrewList(shipVisitFlowId);
            returnPage = COOVISITCREW;
        }
        if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
            returnPage = COOVISITSTAFF;
        }
        return new JSP(returnPage);      
    }
    
    public Resolution getImoTonnage() {
        String result = "";
        
        String idVes = getContext().getRequest().getParameter("id");       
        try{
            Vessel ves = vesselDAO.findById(Integer.parseInt(idVes));
            result = ves.getImo()+"|"+ves.getGross();
            LOG.debug("getImoTonnage : "+result);
        }catch(Exception ex){
            ex.printStackTrace(); // for development used only
            LOG.error("getImoTonnage : ex : "+ ex.getMessage());
            result = "error";
        }
        return new StreamingResolution("text/plan",result);
    }
    
    public Resolution viewEdit(){
        String returnPage = EDITOTHER;
        User user = getContext().getCurrentUser();
        LOG.debug("currentYear : "+currentYear);
        LOG.debug("staffId : "+staffId);
        
        listShipVisitReject = svFlowManager.getListVisitRejected(user, currentYear);
        listCounter = svFlowManager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter);
        for (ShipVisitFlowCounter c : listCounter) {
            counterId = c.getSvFlowCounterId();
        }
        
        String idsvf = getContext().getRequest().getParameter("idsvf");
        try{
            shipVisitFlow = shipVisitFlowDAO.findById(Integer.parseInt(idsvf));
            shipVisitFlowId = String.valueOf(shipVisitFlow.getShipVisitFlowId());
//            returnPage = EDITSTAFF;
            if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.crew.key"))){
                this.initCrewList(String.valueOf(shipVisitFlow.getShipVisitFlowId()));
                listShipVisitCrew = svFlowManager.listCrew(null, String.valueOf(shipVisitFlow.getShipVisitFlowId()));
                this.sizeListCrew = listShipVisitCrew.size();
                
                returnPage = EDITCREW;
            }
            if(shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                shipVisitFlow.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
                returnPage = EDITSTAFF;
            }
            listHistory = svFlowManager.listHistory(String.valueOf(shipVisitFlow.getShipVisitFlowId()));
            LOG.debug("viewEdit : listHistory.size : "+listHistory.size());
        }catch(Exception ex){
            ex.printStackTrace(); // for developer used only
            LOG.error("viewEdit : ex :"+ex.getMessage());
            addSimpleError(svconf.getProperty("super.coo.err.ex"));
            returnPage = VISITREJECT;
        }
        return new JSP(returnPage);
    }
    
    public Resolution saveEdit(){
        String returnPage = EDITOTHER;
        User user = getContext().getCurrentUser();
        LOG.debug("user :"+user.getAdName());
        LOG.debug("currentYear : "+currentYear);
        LOG.debug("staffId : "+staffId);
        LOG.debug("shipVisitFlowId :"+shipVisitFlowId);
        
        try{
            ShipVisitFlow svf = shipVisitFlowDAO.findById(Integer.parseInt(shipVisitFlowId));
            InfoAudit ia = svf.getInfoAudit();
            ia.setUpdateId(user);
            ia.setUpdateDate(new Date());
            svf.setInfoAudit(ia);
            svf.setStatus("K");
            if(svf.getCurrentDepartment().contains(svconf.getProperty("user.crew.key")))
                svf.setSummary(shipVisitFlow.getSummary());
            else if(svf.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                svf.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key"))){
//                svf.setAccomodation(StringEscapeUtils.escapeXml(shipVisitFlow.getAccomodation()));
//                svf.setConclusion(StringEscapeUtils.escapeXml(shipVisitFlow.getConclusion()));
//                svf.setCrewMorale(StringEscapeUtils.escapeXml(shipVisitFlow.getCrewMorale()));
//                svf.setDeck(StringEscapeUtils.escapeXml(shipVisitFlow.getDeck()));
//                svf.setEngine(StringEscapeUtils.escapeXml(shipVisitFlow.getEngine()));
//                svf.setFoodMeal(StringEscapeUtils.escapeXml(shipVisitFlow.getFoodMeal()));
//                svf.setGeneralCondition(StringEscapeUtils.escapeXml(shipVisitFlow.getGeneralCondition()));
//                svf.setOther(StringEscapeUtils.escapeXml(shipVisitFlow.getOther()));
//                svf.setPurpose(StringEscapeUtils.escapeXml(shipVisitFlow.getPurpose()));
//                svf.setRecommend(StringEscapeUtils.escapeXml(shipVisitFlow.getRecommend()));
                svf.setAccomodation(shipVisitFlow.getAccomodation());
                svf.setConclusion(shipVisitFlow.getConclusion());
                svf.setCrewMorale(shipVisitFlow.getCrewMorale());
                svf.setDeck(shipVisitFlow.getDeck());
                svf.setEngine(shipVisitFlow.getEngine());
                svf.setFoodMeal(shipVisitFlow.getFoodMeal());
                svf.setGeneralCondition(shipVisitFlow.getGeneralCondition());
                svf.setOther(shipVisitFlow.getOther());
                svf.setPurpose(shipVisitFlow.getPurpose());
                svf.setRecommend(shipVisitFlow.getRecommend());
            }else
                svf.setDescription(shipVisitFlow.getDescription());
            
            String result = svFlowManager.saveOrUpdateVisit(svf);
            if(result.equals("success")){
                addSimpleMessage(svconf.getProperty("super.coo.success"));
                returnPage = VISITREJECT;
                shipVisitFlowCounter = svFlowManager.getListCounter(svf.getUser(), currentYear, departmentId).get(0);
                InfoAudit iac = shipVisitFlowCounter.getInfoAudit();
                iac.setUpdateId(user);
                iac.setUpdateDate(new Date());
                shipVisitFlowCounter.setRejected(shipVisitFlowCounter.getRejected()-1);
                shipVisitFlowCounter.setInfoAudit(iac);
                svFlowManager.saveOrUpdateVisitCounter(shipVisitFlowCounter);
                return new RedirectResolution(ShipVisitFlowAction.class,"visitReject");
            }else{
                 addSimpleError(svconf.getProperty("super.coo.error"));
                 if(svf.getCurrentDepartment().contains(svconf.getProperty("user.crew.key")))
                     returnPage = EDITCREW;
                 if(svf.getCurrentDepartment().contains(svconf.getProperty("user.super.key")) || 
                    svf.getCurrentDepartment().contains(svconf.getProperty("user.procurement.key")))
                     returnPage = EDITSTAFF;
            }            
        }catch(Exception ex){
            ex.printStackTrace(); // for developer used only
            LOG.error("viewEdit : ex :"+ex.getMessage());
            addSimpleError(svconf.getProperty("super.coo.err.ex"));
            if(user.getDepartment().getDepartmentName().contains(svconf.getProperty("user.crew.key")))
                     returnPage = EDITCREW;
            if(user.getDepartment().getDepartmentName().contains(svconf.getProperty("user.super.key")) || 
                user.getDepartment().getDepartmentName().contains(svconf.getProperty("user.procurement.key")))
                     returnPage = EDITSTAFF;
        }
        
        return new JSP(returnPage);
    }
    
    public Resolution saveCrewAction(){
        String result = "";
        User user = getContext().getCurrentUser();
        String idCrew = getContext().getRequest().getParameter("idcrew");
        try{
            ShipVisitCrew svc = shipVisitCrewDAO.findById(Integer.parseInt(idCrew));
            InfoAudit ia = svc.getInfoAudit();
            ia.setUpdateId(user);
            ia.setUpdateDate(new Date());
            if(rejectNote != null){                
                svc.setRemarks(rejectNote);
            }
            if(status != null){
                ShipVisitCompliance svcp = shipVisitComplianceDAO.findById(status);
                svc.setSvCompId(svcp);
            }
            svc.setInfoAudit(ia);
            result = svFlowManager.saveCrew(svc);
            
        }catch(Exception ex){
            ex.printStackTrace(); // for developer used only
            LOG.error("saveCrewAction : ex :"+ex.getMessage());
            result = svconf.getProperty("super.coo.err.ex");
//            addSimpleError(svconf.getProperty("super.coo.err.ex"));
        }
        return new StreamingResolution("text/plan",result);
    }

    public boolean isCrewDetails() {
        return crewDetails;
    }

    public void setCrewDetails(boolean crewDetails) {
        this.crewDetails = crewDetails;
    }

    public ShipVisitFlow getShipVisitFlow() {
        return shipVisitFlow;
    }

    public void setShipVisitFlow(ShipVisitFlow shipVisitFlow) {
        this.shipVisitFlow = shipVisitFlow;
    }

    public String getCurrentYear() {
        return currentYear;
    }

    public void setCurrentYear(String currentYear) {
        this.currentYear = currentYear;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public List<ShipVisitFlowCounter> getListCounter() {
        return listCounter;
    }

    public void setListCounter(List<ShipVisitFlowCounter> listCounter) {
        this.listCounter = listCounter;
    }

    public ShipVisitFlowCounter getShipVisitFlowCounter() {
        return shipVisitFlowCounter;
    }

    public void setShipVisitFlowCounter(ShipVisitFlowCounter shipVisitFlowCounter) {
        this.shipVisitFlowCounter = shipVisitFlowCounter;
    }

    public Integer getDefaultCount() {
        return defaultCount;
    }

    public void setDefaultCount(Integer defaultCount) {
        this.defaultCount = defaultCount;
    }

    public Integer getCounterId() {
        return counterId;
    }

    public void setCounterId(Integer counterId) {
        this.counterId = counterId;
    }

    public String getShipVisitFlowId() {
        return shipVisitFlowId;
    }

    public void setShipVisitFlowId(String shipVisitFlowId) {
        this.shipVisitFlowId = shipVisitFlowId;
    }

    public boolean isSuperiorView() {
        return superiorView;
    }

    public void setSuperiorView(boolean superiorView) {
        this.superiorView = superiorView;
    }

    public boolean isStaffView() {
        return staffView;
    }

    public void setStaffView(boolean staffView) {
        this.staffView = staffView;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getRejectNote() {
        return rejectNote;
    }

    public void setRejectNote(String rejectNote) {
        this.rejectNote = rejectNote;
    }

    public String getStaffId() {
        return staffId;
    }

    public void setStaffId(String staffId) {
        this.staffId = staffId;
    }

    public FileBean getDocument() {
        return document;
    }

    public void setDocument(FileBean document) {
        this.document = document;
    }

    public ShipVisitFlowFileTmp getShipVisitFlowFileTmp() {
        return shipVisitFlowFileTmp;
    }

    public void setShipVisitFlowFileTmp(ShipVisitFlowFileTmp shipVisitFlowFileTmp) {
        this.shipVisitFlowFileTmp = shipVisitFlowFileTmp;
    }

    public String getVisitId() {
        return visitId;
    }

    public void setVisitId(String visitId) {
        this.visitId = visitId;
    }

    public List<ShipVisitFlow> getListShipVisitKIV() {
        return listShipVisitKIV;
    }

    public void setListShipVisitKIV(List<ShipVisitFlow> listShipVisitKIV) {
        this.listShipVisitKIV = listShipVisitKIV;
    }

    public List<ShipVisitFlow> getListShipVisitApprove() {
        return listShipVisitApprove;
    }

    public void setListShipVisitApprove(List<ShipVisitFlow> listShipVisitApprove) {
        this.listShipVisitApprove = listShipVisitApprove;
    }

    public List<ShipVisitFlow> getListShipVisitReject() {
        return listShipVisitReject;
    }

    public List<ShipVisitFlow> getListStaffVisitKIV() {
        return listStaffVisitKIV;
    }

    public List<ShipVisitFlowFileTmp> getListVisitFiletmp() {
        return listVisitFiletmp;
    }

    public void setListVisitFiletmp(List<ShipVisitFlowFileTmp> listVisitFiletmp) {
        this.listVisitFiletmp = listVisitFiletmp;
    }

    public void setListStaffVisitKIV(List<ShipVisitFlow> listStaffVisitKIV) {
        this.listStaffVisitKIV = listStaffVisitKIV;
    }

    public void setListShipVisitReject(List<ShipVisitFlow> listShipVisitReject) {
        this.listShipVisitReject = listShipVisitReject;
    }

    public List<ShipVisitFlowFile> getListVisitFile() {
        return listVisitFile;
    }

    public void setListVisitFile(List<ShipVisitFlowFile> listVisitFile) {
        this.listVisitFile = listVisitFile;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getForXlsx() {
        return forXlsx;
    }

    public void setForXlsx(String forXlsx) {
        this.forXlsx = forXlsx;
    }

    public String getForDocx() {
        return forDocx;
    }

    public void setForDocx(String forDocx) {
        this.forDocx = forDocx;
    }

    public String getForPptx() {
        return forPptx;
    }

    public void setForPptx(String forPptx) {
        this.forPptx = forPptx;
    }

    public String getForXls() {
        return forXls;
    }

    public void setForXls(String forXls) {
        this.forXls = forXls;
    }

    public String getForDoc() {
        return forDoc;
    }

    public void setForDoc(String forDoc) {
        this.forDoc = forDoc;
    }

    public String getForPpt() {
        return forPpt;
    }

    public void setForPpt(String forPpt) {
        this.forPpt = forPpt;
    }

    public String getForPdf() {
        return forPdf;
    }

    public void setForPdf(String forPdf) {
        this.forPdf = forPdf;
    }

    public String getForVideoMov() {
        return forVideoMov;
    }

    public void setForVideoMov(String forVideoMov) {
        this.forVideoMov = forVideoMov;
    }

    public String getForVideoMkv() {
        return forVideoMkv;
    }

    public void setForVideoMkv(String forVideoMkv) {
        this.forVideoMkv = forVideoMkv;
    }

    public String getForVideoMp4() {
        return forVideoMp4;
    }

    public void setForVideoMp4(String forVideoMp4) {
        this.forVideoMp4 = forVideoMp4;
    }

    public String getForVideoAvi() {
        return forVideoAvi;
    }

    public void setForVideoAvi(String forVideoAvi) {
        this.forVideoAvi = forVideoAvi;
    }

    public String getForVideoMpeg() {
        return forVideoMpeg;
    }

    public void setForVideoMpeg(String forVideoMpeg) {
        this.forVideoMpeg = forVideoMpeg;
    }

    public String getForVideoWmv() {
        return forVideoWmv;
    }

    public void setForVideoWmv(String forVideoWmv) {
        this.forVideoWmv = forVideoWmv;
    }

    public String getForVideoAsf() {
        return forVideoAsf;
    }

    public void setForVideoAsf(String forVideoAsf) {
        this.forVideoAsf = forVideoAsf;
    }

    public String getForVideoFlv() {
        return forVideoFlv;
    }

    public void setForVideoFlv(String forVideoFlv) {
        this.forVideoFlv = forVideoFlv;
    }

    public String getForVideoRmvb() {
        return forVideoRmvb;
    }

    public void setForVideoRmvb(String forVideoRmvb) {
        this.forVideoRmvb = forVideoRmvb;
    }

    public String getForVideo3gp() {
        return forVideo3gp;
    }

    public void setForVideo3gp(String forVideo3gp) {
        this.forVideo3gp = forVideo3gp;
    }    

    public List<ShipVisitCrew> getListShipVisitCrew() {
        return listShipVisitCrew;
    }

    public void setListShipVisitCrew(List<ShipVisitCrew> listShipVisitCrew) {
        this.listShipVisitCrew = listShipVisitCrew;
    }

    public List<ShipVisitDescription> getListShipVisitDescription() {
        return listShipVisitDescription;
    }

    public void setListShipVisitDescription(List<ShipVisitDescription> listShipVisitDescription) {
        this.listShipVisitDescription = listShipVisitDescription;
    }

    public List<String> getCompliances() {
        return compliances;
    }

    public void setCompliances(List<String> compliances) {
        this.compliances = compliances;
    }

    public List<String> getRemarks() {
        return remarks;
    }

    public void setRemarks(List<String> remarks) {
        this.remarks = remarks;
    }

    public Integer getSizeDesc() {
        return sizeDesc;
    }

    public void setSizeDesc(Integer sizeDesc) {
        this.sizeDesc = sizeDesc;
    }

    public List<ShipVisitCrew> getListCrewCoo1() {
        return listCrewCoo1;
    }

    public void setListCrewCoo1(List<ShipVisitCrew> listCrewCoo1) {
        this.listCrewCoo1 = listCrewCoo1;
    }

    public List<ShipVisitCrew> getListCrewCoo2() {
        return listCrewCoo2;
    }

    public void setListCrewCoo2(List<ShipVisitCrew> listCrewCoo2) {
        this.listCrewCoo2 = listCrewCoo2;
    }

    public List<ShipVisitCrew> getListCrewCoo3() {
        return listCrewCoo3;
    }

    public void setListCrewCoo3(List<ShipVisitCrew> listCrewCoo3) {
        this.listCrewCoo3 = listCrewCoo3;
    }

    public List<ShipVisitCrew> getListCrewCoo4() {
        return listCrewCoo4;
    }

    public void setListCrewCoo4(List<ShipVisitCrew> listCrewCoo4) {
        this.listCrewCoo4 = listCrewCoo4;
    }

    public List<ShipVisitCrew> getListCrewCoo5() {
        return listCrewCoo5;
    }

    public void setListCrewCoo5(List<ShipVisitCrew> listCrewCoo5) {
        this.listCrewCoo5 = listCrewCoo5;
    }

    public List<ShipVisitCrew> getListCrewCoo6() {
        return listCrewCoo6;
    }

    public void setListCrewCoo6(List<ShipVisitCrew> listCrewCoo6) {
        this.listCrewCoo6 = listCrewCoo6;
    }

    public List<ShipVisitCrew> getListCrewCoo7() {
        return listCrewCoo7;
    }

    public void setListCrewCoo7(List<ShipVisitCrew> listCrewCoo7) {
        this.listCrewCoo7 = listCrewCoo7;
    }

    public List<ShipVisitCrew> getListCrewCoo8() {
        return listCrewCoo8;
    }

    public void setListCrewCoo8(List<ShipVisitCrew> listCrewCoo8) {
        this.listCrewCoo8 = listCrewCoo8;
    }

    public List<ShipVisitCrew> getListCrewCoo9() {
        return listCrewCoo9;
    }

    public void setListCrewCoo9(List<ShipVisitCrew> listCrewCoo9) {
        this.listCrewCoo9 = listCrewCoo9;
    }

    public List<ShipVisitCrew> getListCrewCoo10() {
        return listCrewCoo10;
    }

    public void setListCrewCoo10(List<ShipVisitCrew> listCrewCoo10) {
        this.listCrewCoo10 = listCrewCoo10;
    }

    public List<ShipVisitCrew> getListCrewCoo11() {
        return listCrewCoo11;
    }

    public void setListCrewCoo11(List<ShipVisitCrew> listCrewCoo11) {
        this.listCrewCoo11 = listCrewCoo11;
    }

    public List<ShipVisitCrew> getListCrewCoo12() {
        return listCrewCoo12;
    }

    public void setListCrewCoo12(List<ShipVisitCrew> listCrewCoo12) {
        this.listCrewCoo12 = listCrewCoo12;
    }

    public List<ShipVisitCrew> getListCrewCoo13() {
        return listCrewCoo13;
    }

    public void setListCrewCoo13(List<ShipVisitCrew> listCrewCoo13) {
        this.listCrewCoo13 = listCrewCoo13;
    }

    public List<ShipVisitCrew> getListCrewCoo14() {
        return listCrewCoo14;
    }

    public void setListCrewCoo14(List<ShipVisitCrew> listCrewCoo14) {
        this.listCrewCoo14 = listCrewCoo14;
    }

    public List<ShipVisitFlow> getListStaffStatusDetails() {
        return listStaffStatusDetails;
    }

    public void setListStaffStatusDetails(List<ShipVisitFlow> listStaffStatusDetails) {
        this.listStaffStatusDetails = listStaffStatusDetails;
    }

    public User getUserSelect() {
        return userSelect;
    }

    public void setUserSelect(User userSelect) {
        this.userSelect = userSelect;
    }

    public String getStatusDesc() {
        return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
        this.statusDesc = statusDesc;
    }

    public User getUserCurrSession() {
        return userCurrSession;
    }

    public void setUserCurrSession(User userCurrSession) {
        this.userCurrSession = userCurrSession;
    }

    public List<ShipVisitFlowHistory> getListHistory() {
        return listHistory;
    }

    public void setListHistory(List<ShipVisitFlowHistory> listHistory) {
        this.listHistory = listHistory;
    }

    public Integer getSizeListCrew() {
        return sizeListCrew;
    }

    public void setSizeListCrew(Integer sizeListCrew) {
        this.sizeListCrew = sizeListCrew;
    }

    public ShipVisitFlowFile getShipVisitFlowFile() {
        return shipVisitFlowFile;
    }

    public void setShipVisitFlowFile(ShipVisitFlowFile shipVisitFlowFile) {
        this.shipVisitFlowFile = shipVisitFlowFile;
    }
}
